package msg;

import static org.junit.Assert.*;

import javax.xml.parsers.*;

import msg.visitors.MessageToXML;

import org.junit.*;
import org.w3c.dom.*;

import common.DALTests;

@SuppressWarnings({"javadoc", "static-method"})
public class MessageToXMLTest extends DALTests {
	private static Document		doc;
	private static MessageToXML	$;
	
	@BeforeClass
	public static void setupBeforeClass() throws Exception {
		DALTests.setupBeforeClass();
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder;
		documentBuilder = documentBuilderFactory.newDocumentBuilder();
		doc = documentBuilder.newDocument();
		$ = new MessageToXML(doc);
	}
	
	@Test
	public void testMessage() throws Exception {
		Message msg = new Message(0, new Location(0.5, 0.5), "Message content", getUser());
		Element element = msg.accept($);
		// String str = elementToString(element);
		// System.out.println(str);
		assertEquals(element.getAttribute("id"), msg.getId().toString());
		Element location = (Element)element.getChildNodes().item(0);
		assertEquals(location.getAttribute("lat"), msg.getLocation().getLatitude() + "");
		assertEquals(location.getAttribute("long"), msg.getLocation().getLongitude() + "");
		Element content = (Element)element.getChildNodes().item(1);
		assertEquals(content.getTextContent(), msg.getContent());
		Element poster = (Element)element.getChildNodes().item(2);
		assertEquals(poster.getAttribute("id"), msg.getPoster().getId());
		Element time = (Element)element.getChildNodes().item(3);
		assertEquals(time.getAttribute("time"), msg.getTime() + "");
	}
	
	@Test
	public void testEvent() throws Exception {
		EventMessage event = new EventMessage(0, new Location(0.5, 0.5), "Event content", getUser(),
				getTomorrow(), 10);
		Element eventElement = event.accept($);
		// String str = elementToString(eventElement);
		// System.out.println(str);
		Element element = (Element)eventElement.getChildNodes().item(0);
		assertEquals(element.getAttribute("id"), event.getId().toString());
		Element location = (Element)element.getChildNodes().item(0);
		assertEquals(location.getAttribute("lat"), event.getLocation().getLatitude() + "");
		assertEquals(location.getAttribute("long"), event.getLocation().getLongitude() + "");
		Element content = (Element)element.getChildNodes().item(1);
		assertEquals(content.getTextContent(), event.getContent());
		Element poster = (Element)element.getChildNodes().item(2);
		assertEquals(poster.getAttribute("id"), event.getPoster().getId());
		Element time = (Element)element.getChildNodes().item(3);
		assertEquals(time.getAttribute("time"), event.getTime() + "");
		Element capacity = (Element)eventElement.getChildNodes().item(1);
		assertEquals(capacity.getAttribute("number"), event.getCapacity() + "");
		
	}
}
